package com.tim.soundseechina;

import org.apache.log4j.Logger;

import com.tim.soundseechina.entity.InformationInfo;

public class Nationality extends Thread {
	private static final Logger log = Logger.getLogger(Nationality.class);
	private static final String MODEL_NAME = "nationality/";

	public void run() {
		// 56个民族
		String nationalityUrl = SoundSeeChina.URL + "/cn/custom_ethnic.aspx";

		InformationInfo info = new InformationInfo();
		// 56个民族类型
		info.setMenuId(2);

		// 获取页面信息
		String content = getConent(nationalityUrl);

		// 获取民族信息
		getPagerInfo(info, content);

		// 获取分页地址
		String pagerStr = getPagerString(content);
		// 有分页操作
		if (pagerStr != null) {
			// 获取分页相关信息
			String pagerNo = pagerStr.substring(pagerStr.lastIndexOf("=") + 1,
					pagerStr.lastIndexOf("\""));
			// 第二页起抓
			for (int i = 1; i < Integer.parseInt(pagerNo); i++) {
				getPagerInfo(info, getConent(nationalityUrl + "?page="
						+ (i + 1)));
			}
		}
		log.warn("56个名族抓取完成");
	}

	public static void getPagerInfo(InformationInfo info, String content) {
		String temp = StringUtil
				.getStringFromPattern(
						content,
						"(.*)<!-- AspNetPager V7.0 for VS2008  Copyright:2003-2007 Webdiyer \\(www.webdiyer.com\\) -->");
		String[] tempArr = temp.split("<div class=\"custom_ethnic_list\">");
		for (int i = 0; i < tempArr.length; i++) {
			InformationInfo dataInfo = new InformationInfo();
			// 获取信息页面地址
			String infoUrl = StringUtil.getStringFromPattern(tempArr[i],
					"<a href='(.*)'>");
			// 中文信息页url
			String url = SoundSeeChina.URL + "/cn/" + infoUrl;
			// 获取中文信息
			String tempInfo = getInformationTemp(dataInfo, url);
			// 获取标题
			String title = getInformationTitle(tempInfo);
			// 设置类型
			dataInfo.setMenuId(info.getMenuId());
			// 设置中文类型
			dataInfo.setLanType("cn");
			dataInfo.setStatus((short) 0);
			// 设置标题
			dataInfo.setTitle(title);
			// 抓取设置内容
			dataInfo.setInformationDesc(getInformationContent(tempInfo));
			// 抓取图片列表
			dataInfo.setImgSrc(getImgListInfo(infoUrl));
			// 获取首个图片地址
			String indexImgStr = StringUtil.getStringFromPattern(tempArr[i],
					"<img src='..(.*)' width");
			String fileType = indexImgStr.substring(indexImgStr
					.lastIndexOf("."));
			// 图片保存路径
			String imgFile = MODEL_NAME + System.currentTimeMillis() + fileType;
			// 下载图片
			HttpClientProc.getUrl2File(SoundSeeChina.URL + indexImgStr,
					SoundSeeChina.CHINESE_CULTURE + imgFile);
			// 设置首页图片路径
			dataInfo.setIndexImg(imgFile);
			// 中文数据入库
			SoundSeeChina.informationInfoService.create(dataInfo);

			// 英文信息
			InformationInfo enInfo = new InformationInfo();
			// 英文信息页url
			url = SoundSeeChina.URL + "/en/" + infoUrl;
			tempInfo = getInformationTemp(enInfo, url);
			// 获取标题
			title = getInformationTitle(tempInfo);
			// 设置图片连接
			enInfo.setImgSrc(dataInfo.getImgSrc());
			enInfo.setMenuId(dataInfo.getMenuId());
			enInfo.setTypeId(dataInfo.getTypeId());
			enInfo.setSubTypeStr(dataInfo.getSubTypeStr());
			enInfo.setStatus(dataInfo.getStatus());
			enInfo.setIndexImg(dataInfo.getIndexImg());
			enInfo.setLanType("en");
			enInfo.setTitle(title);
			// 抓取设置内容
			enInfo.setInformationDesc(getInformationContent(tempInfo));
			// 英文数据入库
			SoundSeeChina.informationInfoService.create(enInfo);
		}
	}

	public static String getInformationTemp(InformationInfo info, String url) {
		// 设置来源页
		info.setFromLink(url);
		// 获取页面信息
		String tempStr = HttpClientProc.getUrl2String(url,
				SoundSeeChina.CHAR_SET);
		return StringUtil
				.getStringFromPattern(tempStr,
						"id=\"three-con-right-top\">(.*)<div class=\"four-ethnic-bottom-top\">");
	}

	// 获取资讯标题
	public static String getInformationTitle(String content) {
		return StringUtil
				.getStringFromPattern(
						content,
						"<span style=\"color: #0183fd; font-size: 14px; font-weight: bold;\">(.*)</span><br />");
	}

	// 获取资讯内容
	public static String getInformationContent(String content) {
		String temp = StringUtil.getStringFromPattern(content, "<P(.*)</P>");
		temp = StringUtil.getStringFromPattern(temp, ">(.*)");
		return temp;
	}

	// 获取图片列表信息
	@SuppressWarnings("static-access")
	public static String getImgListInfo(String url) {
		// 获取页面编号
		String pageNo = url.substring(url.lastIndexOf("_") + 1, url
				.lastIndexOf("."));
		url = SoundSeeChina.URL + "/cn/custom/nationimages.ashx?Name=" + pageNo;
		// 获取页面信息
		String tempStr = HttpClientProc.getUrl2String(url,
				SoundSeeChina.CHAR_SET);
		if (tempStr == null || "".equals(tempStr)) {
			return null;
		} else {
			StringBuilder imgPathStr = new StringBuilder();
			String[] tempStrArr = tempStr.split("</li><li");
			for (int i = 0; i < tempStrArr.length; i++) {
				String imgUrl = StringUtil.getStringFromPattern(tempStrArr[i],
						"src=\"../..(.*)\" alt=.*");
				String fileType = imgUrl.substring(imgUrl.lastIndexOf("."));
				String imgFile = MODEL_NAME + System.currentTimeMillis()
						+ fileType;
				// 下载图片
				HttpClientProc.getUrl2File(SoundSeeChina.URL + imgUrl,
						SoundSeeChina.CHINESE_CULTURE + imgFile);
				try {
					Thread.currentThread().sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if (i != 0) {
					imgPathStr.append(",");
				}
				imgPathStr.append(imgFile);
			}
			return imgPathStr.toString();
		}
	}

	public static String getConent(String url) {
		String content = HttpClientProc.getUrl2String(url,
				SoundSeeChina.CHAR_SET);
		content = StringUtil
				.getStringFromPattern(
						content,
						"<div class=\"custom_ethnic_list\">(.*)<!-- AspNetPager V7.0 for VS2008 End -->");
		return content;
	}

	public static String getPagerString(String content) {
		return StringUtil.getStringFromPattern(content,
				"class=\"megas512\">(.*)</div>");
	}
}
